const app = getApp();

import bData from '../../service/mock.js'
Page({
  data: {
    contentHeight: app.globalData.windowHeight,
    TabCur: null,
    themes: [],
    items: [],
    pigeSize: 6,
    pageNum: 1
  },
  // 页面加载
  onLoad: function (options) {
    var that = this;
    
    var i = bData.getData(this.data.pageNum);
    this.setData({ TabCur: i.themes[0].id, themes: i.themes, items: i.items });

    this._doRefreshMasonry(this.data.items)
  },

  // 拉触底加载
  onReachBottom: function () {
    var pageNo = this.data.pageNum + 1;
    var data = bData.getData(pageNo, this.data.TabCur);
    if (data.items && data.items.length > 0 ) {
      this.setData({
        pageNum: pageNo
      })
      this._doAppendMasonry(data.items)
    } 
  },

  // 刷新数据
  _doRefreshMasonry(items) {
    this.masonryListComponent = this.selectComponent('#masonry');
    if (this.masonryListComponent) {
      this.masonryListComponent.start(items).then(() => {
        //console.log('刷新 completed')
      })
    }
  },

  // 追加数据
  _doAppendMasonry(items) {
    this.masonryListComponent = this.selectComponent('#masonry')
    this.masonryListComponent.append(items).then(() => {
      //console.log('追加 completed')
    })
  },

  // tab选择事件
  tabSelect(e) {
    var id = e.currentTarget.dataset.id;
    var data = bData.getData(1, id);
    this.setData({
      TabCur: id,
      items: data.items,
      pageNum:1
    })

    this._doRefreshMasonry([])
    this._doRefreshMasonry(this.data.items);
  }
})